﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WorkWithFile
{
    class FileBuilder
    {
        public FileBuilder(string path)
        {
            this.path = path;
        } 

        /// <summary>
        /// Method for creation the file
        /// </summary>
        private void CreateFile()
        {
            if (!File.Exists(this.path))
            {
                using (File.Create(path)) { }
            }
        }
        
        /// <summary>
        /// Method for writing text to the file
        /// </summary>
        public void WriteToFile(string mes)
        {
            CreateFile();

            using (FileStream fileStream = File.OpenWrite(path))
            {
                byte[] buff = Encoding.Unicode.GetBytes(mes);
                fileStream.Write(buff, 0, buff.Length);
            }
        }

        /// <summary>
        /// Method for reading text from the file
        /// </summary>
        public string ReadFromFile()
        {
            string str;

            if (File.Exists(path))
            {
                using (StreamReader reader = new StreamReader(path, Encoding.Unicode))
                {
                    str = reader.ReadToEnd();
                }
            }
            else
            {
                str = (new FileNotFoundException()).Message;
            }

            return str;
        }

        private string path;
    }
}
